let pagenum = 1

// !  渲染--我的点赞
function renderZan() {
    axios.get('/user/userDataList?page=1&limit=10&type=2')
        .then(({
            data: {
                data
            }
        }) => {
            // console.log(data)
            const {
                count
              } = data.list
            const rendering = data.list.data.map((item) => {
                const {
                    title,
                    id,
                    pic,
                    lovenum
                } = item
                let picIng = `http://124.223.14.236:8060/${pic}`
                return `
          <li>
            <a href="javascript:;" >
              <span class="thumbnail">
                <img class="thumb" src="${pic===''?'./images/default.png':picIng}" />
              </span>
              <span class="text">${title}</span>
              <span class="text-muted post-views">已点赞(${lovenum})</span>
              <button class="btn btn-xs btn-danger" data-id='${id}'><i class="fa fa-trash"></i>&nbsp;取消点赞</button>
            </a>
        </li>
        `
            }).join('')
            $('.items-01').html(rendering)
            renderfen(count)
        })
}
renderZan()

// todo  渲染分页功能函数
function renderfen(count){
    layui.use(function () {
      // 开启 HASH
      laypage.render({
        elem: 'aa',
        count: count,
        theme: '#1E9FFF',
        curr: pagenum, // 初始获取 hash 值为 curr 的当前页
        layout: ['prev', 'page', 'next', 'count','refresh'],
        jump: function (obj, first) {
          //  得到当页值
          pagenum = obj.curr
          // 判断下一页 下一页是否显示
          if (pagenum === 1) {
            document.querySelector('.layui-laypage-prev').style.display = 'none'
          } else if (document.querySelector('.layui-disabled')) {
            document.querySelector('.layui-disabled').style.display = 'none'
          }
          //首次不执行
          if (!first) {
            //do somethingre
            renderZan(obj.curr)
          }
        }
      });
    });
  }

// ! 取消点赞
// 事件委托获取id
$('.items-01').on('click', 'button', function () {
    const idZan = $(this).data('id')
    console.log(idZan)
    axios.get(`/user/userDataHandle?type=2&action=del&article_id=${idZan}`)
    .then((res) => {
        const {data ,errno} = res.data
        if (errno !== 0) {
            return layer.msg('取消点赞失败')
        }
        // 成功
        layer.msg(data, {
            icon: 1,
            time: 500 // 设置 2 秒后自动关闭
        }, function () {
            // 关闭后 跳转操作
            location.href = '../public/user-zan.html'
        })
    })
})